package cn.com.bluemoon.daps.model.controller;


import cn.com.bluemoon.common.KeyAnnotationClz;
import cn.com.bluemoon.common.UserPermission;
import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.domain.UserInfoHolder;
import cn.com.bluemoon.daps.common.exception.DapException;
import cn.com.bluemoon.daps.model.core.file.report.ImportReport;
import cn.com.bluemoon.daps.model.service.DapDataModelImportService;
import cn.com.bluemoon.daps.model.service.DapDataModelService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 模型导入 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-02-02
 */
@Api(tags = "模型导入")
@RestController
@Slf4j
@RequestMapping("/modelImport")
@BmPermission(value = "模型导入功能")
@Validated
public class DapDataModelImportController {

    @Autowired
    private DapDataModelImportService importService;

    @Resource
    private DapDataModelService modelService;

    /**
     * 基于模型id来获取对应数据库类的魔板
     */
    @GetMapping("/download/excel")
    @ApiOperation("下载excel模板")
    @BmPermission(value = "model:table:import", name = "下载excel模板", queryTokenPerm = true)
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = Void.class)})
    @UserPermission.SingleRow(keyFormClz = KeyAnnotationClz.RequestParam, key = "modelId", errorMsg = "只允许模型授权者进行下载模板功能")
    public void downloadTemplate(@ApiParam("模型id") @RequestParam("modelId") String modelId, HttpServletResponse response) {
        importService.downloadTemplateByModelId(modelId, response);
    }

    /**
     * 基于模型id来获取对应数据库类的魔板
     */
    @GetMapping("/download/excelV2")
    @ApiOperation("下载excel模板")
    @BmPermission(value = "model:table:import", name = "下载excel模板", queryTokenPerm = true)
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = Void.class)})
    @UserPermission.SingleRow(keyFormClz = KeyAnnotationClz.RequestParam, key = "modelId", errorMsg = "只允许模型授权者进行下载模板功能")
    public void downloadTemplateV2(@ApiParam("模型id") @RequestParam("modelId") String modelId, HttpServletResponse response) {
        importService.downloadTemplateV2ByModelId(modelId, response);
    }

    @PostMapping("/import/excelV2")
    @ApiOperation("上传excel表格")
    @BmPermission(value = "model:table:import", name = "上传excel表格", queryTokenPerm = true)
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = ImportReport.class)})
    @UserPermission.SingleRow(keyFormClz = KeyAnnotationClz.RequestParam, key = "modelId", errorMsg = "只允许模型授权者进行导入功能")
    public ResultBean<Boolean> importFile(@RequestParam MultipartFile mFile,
                                          @RequestParam @ApiParam("模型id") String modelId) {
        boolean importReport = importService.fileImportV2(mFile, modelId, UserInfoHolder.getUserInfo().orElseThrow(() -> new DapException("获取用户信息失败")));
        return ResultBean.ok(importReport);
    }

    @PostMapping("/import/excel")
    @ApiOperation("上传excel表格")
    @BmPermission(value = "model:table:import", name = "上传excel表格", queryTokenPerm = true)
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = ImportReport.class)})
    @UserPermission.SingleRow(keyFormClz = KeyAnnotationClz.RequestParam, key = "modelId", errorMsg = "只允许模型授权者进行导入功能")
    public ResultBean<ImportReport> bmImport(@RequestParam MultipartFile mFile,
                                             @RequestParam @ApiParam("模型id") String modelId) {
        ImportReport importReport = importService.fileImport(mFile, modelId);
        return importReport.isResult() ? ResultBean.ok(importReport) : ResultBean.error("文件导入失败！！", importReport);
    }


    @GetMapping("/auth/href")
    @ApiOperation("请求导入相关功能-权限前置检查（由于前端请求为window.href跳转无法利用ajax进行权限认证）")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "请求导入相关功能", response = ResultBean.class)})
    @BmPermission(value = "model:table:import", name = "上传excel表格", queryTokenPerm = true)
    @UserPermission.SingleRow(keyFormClz = KeyAnnotationClz.RequestParam, key = "modelId", errorMsg = "只允许模型授权者进行导入相关功能")
    public ResultBean<String> checkAuthHref(@ApiParam("模型id") @RequestParam("modelId") String modelId) {
        modelService.getByIdAsset(modelId, "模型信息不存在");
        return ResultBean.ok();
    }

}
